package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.ItemPageService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
public class ItemPageServiceImpl implements ItemPageService {

    private static final Logger logger = LoggerFactory.getLogger(ItemPageServiceImpl.class);

    //注入FreeMarkerConfigurer
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Value("${freemarker.outPutDir}")
    private String outPutDir;

    @Reference
    private GoodsService goodsService;

    @Reference
    private ItemCatService itemCatService;

    @Override
    public void getItemPage(Long[] ids) {
        //加载配置
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter = null;
        try {
            for (Long goodsId :ids){
                //创建模板
                Template template = configuration.getTemplate("item.ftl");
                //创建数据模型
                Map map = new HashMap();

                //查看goods信息
                TbGoods tbGoods = goodsService.getGoodsById(goodsId);
                //查看goodsDesc信息
                TbGoodsDesc tbGoodsDesc = goodsService.getGoodsDescById(goodsId);

                map.put("goods",tbGoods);
                map.put("goodsDesc",tbGoodsDesc);

                TbItemCat itemById1 = itemCatService.getItemById(tbGoods.getCategory1Id());
                TbItemCat itemById2 = itemCatService.getItemById(tbGoods.getCategory2Id());
                TbItemCat itemById3 = itemCatService.getItemById(tbGoods.getCategory3Id());

                map.put("itemById1",itemById1.getName());
                map.put("itemById2",itemById2.getName());
                map.put("itemById3",itemById3.getName());

                //查看item信息
                List<TbItem> itemList = itemCatService.getItemByGoodsId(goodsId);

                map.put("itemList",itemList);

                //创建输出对象
                fileWriter = new FileWriter(outPutDir + goodsId + ".html");
                template.process(map,fileWriter);
            }
        } catch (Exception e) {
            logger.error("执行失败",e);
        }finally {//关流
            try {
                if (null != fileWriter){
                    logger.info("执行成功");
                    fileWriter.close();
                }
            } catch (IOException e) {
                logger.error("执行失败",e);
            }
        }
    }
}
